// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: H Fan Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.fanOffset.x

// States
private alias 0 : HFAN_DEACTIVATE
private alias 1 : HFAN_IDLE
private alias 2 : HFAN_ACTIVE

// Player Aliases
private alias object.xpos : player.xpos


// ========================
// Function Declarations
// ========================

reserve function HFan_DebugDraw
reserve function HFan_DebugSpawn


// ========================
// Function Definitions
// ========================

private function HFan_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function HFan_DebugSpawn
	CreateTempObject(TypeName[H Fan], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
	object[tempObjectPos].speed = 48
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case HFAN_DEACTIVATE
		if object.speed > 0
			object.speed--
		else
			object.state++
		end if
		object.frame = object.animationTimer
		object.frame /= 48
		object.animationTimer += object.speed
		object.animationTimer %= 288
		break

	case HFAN_IDLE
		object.timer++
		if object.timer == 30
			object.timer = 0
			object.speed = 48
			object.state++
		end if
		break

	case HFAN_ACTIVE
		object.frame = object.animationTimer
		object.frame /= 48
		object.animationTimer += object.speed
		object.animationTimer %= 288

		object.timer++
		if object.timer == 180
			object.timer = 0
			object.state = HFAN_DEACTIVATE
		end if

		object.fanOffset.x = object.xpos
		if object.direction == FLIP_NONE
			object.fanOffset.x -= 0xA00000
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -160, -112, 32, 112, currentPlayer, 0, 0, 0, 0)
				if checkResult == true
					temp0 = object.fanOffset.x
					temp0 -= player[currentPlayer].xpos
					temp0 >>= 4
					player[currentPlayer].xpos += temp0
				end if
			next
		else
			object.fanOffset.x += 0xA00000
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -32, -112, 160, 112, currentPlayer, 0, 0, 0, 0)
				if checkResult == true
					temp0 = object.fanOffset.x
					temp0 -= player[currentPlayer].xpos
					temp0 >>= 4
					player[currentPlayer].xpos += temp0
				end if
			next
		end if
		break
		
	end switch
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("OOZ/Objects.gif")
	SpriteFrame(-12, -16, 24, 32, 462, 157)
	SpriteFrame(-12, -16, 24, 32, 462, 190)
	SpriteFrame(-12, -16, 24, 32, 462, 223)
	SpriteFrame(-12, -16, 24, 32, 487, 157)
	SpriteFrame(-12, -16, 24, 32, 487, 190)
	SpriteFrame(-12, -16, 24, 32, 487, 223)

	foreach (TypeName[H Fan], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].speed = 36
	next

	SetTableValue(TypeName[H Fan], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(HFan_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(HFan_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	LoadSpriteSheet("OOZ/Objects.gif")
	SpriteFrame(-12, -16, 24, 32, 462, 157)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
